# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
import orderdb
import pymssql
from datetime import datetime

class OrderInputFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.OrderInput_window = tk.Toplevel(self.master)
        self.OrderInput_window.title('订购信息录入')
        self.OrderInput_window.geometry('%dx%d' % (470, 400))
        self.page = tk.Frame(self.OrderInput_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.bno = tk.StringVar()
        self.quantity = tk.StringVar()
        self.amount = tk.StringVar()
        self.order_time = tk.StringVar()
        self.pay_time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.quantity).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购金额: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.amount).grid(row=5, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购时间: ').grid(row=6, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.order_time).grid(row=6, column=1, sticky=tk.E)
        tk.Label(self.page, text='付款时间: ').grid(row=7, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pay_time).grid(row=7, column=1, sticky=tk.E)
        tk.Button(self.page, text='录入订购信息', command=self.record_OrderInput).grid(row=8, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=9, column=1, sticky=tk.E, pady=10)

    def record_OrderInput(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        bno = self.bno.get()
        quantity = self.quantity.get()
        amount = self.amount.get()
        pay_time = self.pay_time.get()
        order_time = self.order_time.get()

        if not ISBN or not aid or not bno or not order_time or not pay_time:
            self.status.set('录入失败，ISBN、职工号、书商号、订购时间、付款时间均不能为空')
            return

        if not orderdb.isbn_exists(ISBN):
            self.status.set('录入失败，ISBN不存在')
            return

        if not orderdb.aid_exists(aid):
            self.status.set('录入失败，职工号不存在')
            return

        if not orderdb.bno_exists(bno):
            self.status.set('录入失败，书商号不存在')
            return

        try:
            order_time = datetime.strptime(order_time, '%Y-%m-%d %H:%M:%S')
            pay_time = datetime.strptime(pay_time, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            self.status.set('录入失败，时间格式不正确')
            return

        if orderdb.order_exists(ISBN, aid, bno, order_time, pay_time):
            self.status.set('录入失败，信息已存在')
            return

        orderdb.insert_order(ISBN, aid, bno, quantity, amount, order_time, pay_time)
        self.status.set('订购信息录入成功')


class OrderQueryFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.OrderQuery_window = tk.Toplevel(self.master)
        self.OrderQuery_window.title('订购信息查询')
        self.OrderQuery_window.geometry('%dx%d' % (470, 400))
        self.page = tk.Frame(self.OrderQuery_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.bno = tk.StringVar()
        self.quantity = tk.StringVar()
        self.amount = tk.StringVar()
        self.order_time = tk.StringVar()
        self.pay_time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.quantity, state='readonly').grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购金额: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.amount, state='readonly').grid(row=5, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购时间: ').grid(row=6, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.order_time).grid(row=6, column=1, sticky=tk.E)
        tk.Label(self.page, text='付款时间: ').grid(row=7, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pay_time).grid(row=7, column=1, sticky=tk.E)
        tk.Button(self.page, text='查询订购信息', command=self.query_OrderInfo).grid(row=8, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=9, column=1, sticky=tk.E, pady=10)

    def query_OrderInfo(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        bno = self.bno.get()
        pay_time = self.pay_time.get()
        order_time = self.order_time.get()

        if not ISBN or not aid or not bno or not pay_time or not order_time:
            self.status.set('查询失败，ISBN、职工号、书商号、付款时间、订购时间均不能为空')
            self.quantity.set('')
            self.amount.set('')
            self.order_time.set('')
            self.pay_time.set('')
            return

        result = orderdb.query_by_ISBN_aid_bno_pay_time_order_time(ISBN, aid, bno, pay_time, order_time)
        if result:
            self.quantity.set(result['quantity'])
            self.amount.set(result['amount'])
            self.order_time.set(result['order time'])
            self.pay_time.set(result['pay time'])
            self.status.set('查询成功')
        else:
            self.quantity.set('')
            self.amount.set('')
            self.order_time.set('')
            self.pay_time.set('')
            self.status.set('没有找到对应的订购信息')



class OrderChangeFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.OrderChange_window = tk.Toplevel(self.master)
        self.OrderChange_window.title('订购信息修改')
        self.OrderChange_window.geometry('%dx%d' % (470, 400))
        self.page = tk.Frame(self.OrderChange_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.bno = tk.StringVar()
        self.quantity = tk.StringVar()
        self.amount = tk.StringVar()
        self.order_time = tk.StringVar()
        self.pay_time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='书商号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.bno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.quantity).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购金额: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.amount).grid(row=5, column=1, sticky=tk.E)
        tk.Label(self.page, text='订购时间: ').grid(row=6, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.order_time).grid(row=6, column=1, sticky=tk.E)
        tk.Label(self.page, text='付款时间: ').grid(row=7, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.pay_time).grid(row=7, column=1, sticky=tk.E)
        tk.Button(self.page, text='修改订购信息', command=self.change_OrderInfo).grid(row=8, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=9, column=1, sticky=tk.E, pady=10)

    def change_OrderInfo(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        bno = self.bno.get()
        pay_time = self.pay_time.get()
        order_time = self.order_time.get()

        data = {
            'aid': aid,
            'bno': bno,
            'quantity': self.quantity.get(),
            'amount': self.amount.get(),
            'order_time': order_time,
            'pay_time': pay_time
        }

        if not ISBN or not aid or not bno:
            self.status.set('修改失败，ISBN、职工号、书商号、订购时间、付款时间均不能为空')
            return

        try:
            result = orderdb.query_by_ISBN_aid_bno_pay_time_order_time(ISBN, aid, bno, pay_time, order_time)
            if result:
                orderdb.update_by_ISBN_aid_bno_order_time_pay_time(ISBN, aid, bno, order_time, pay_time, data)
                self.status.set('修改成功')
            else:
                self.status.set('没有找到对应的订购信息')
        except pymssql.OperationalError as e:
            self.status.set('修改失败,库存数量不足')


class OrderDeleteFrame(tk.Frame):
    def __init__(self, master, tree_view, item,  ISBN, aid, bno,pay_time,order_time):
        super().__init__(master)
        self.master = master
        self.tree_view = tree_view
        self.item = item
        self.ISBN = ISBN
        self.aid = aid
        self.bno = bno
        self.order_time = order_time
        self.pay_time = pay_time
        self.OrderDelete_window = tk.Toplevel(self.master)
        self.OrderDelete_window.title('是否删除')
        self.OrderDelete_window.geometry('220x90')
        self.page = tk.Frame(self.OrderDelete_window)
        self.page.pack()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Button(self.page, text='是', command=self.confirm_delete).grid(row=4, column=0, sticky=tk.E, pady=10)
        tk.Button(self.page, text='否', command=self.cancel_delete).grid(row=4, column=2, sticky=tk.W, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, pady=10)

    def confirm_delete(self):
        try:
            # 尝试执行删除操作
            orderdb.delete_by_ISBN_aid_bno_order_time_pay_time(self.ISBN, self.aid, self.bno, self.order_time, self.pay_time)
            self.tree_view.delete(self.item)

            # 删除成功提示
            self.status.set('删除成功')

            # 如果删除成功，关闭窗口
            self.OrderDelete_window.destroy()
        except pymssql.OperationalError as e:  # 捕获数据库异常
            self.status.set('删除失败，库存数量不足')

    def cancel_delete(self):
        self.OrderDelete_window.destroy()